/*
* 文件：UnityXSettings.cs
* 作者：王阳
* 时间：2019-5-20
* 描述：UnityXSettings
*/

#define INIT

using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace UnityXEditor
{
    public class UnityXSettings : ScriptableObject
    {
        private const string c_AssetPath = "ProjectSettings/UnityXSettings.asset";
        private static UnityXSettings s_Instance = null;
        private static UnityXSettings instance
        {
            get
            {
                if (s_Instance == null)
                {
                    Object[] assets = InternalEditorUtility.LoadSerializedFileAndForget(c_AssetPath);
                    if (assets.Length > 0)
                    {
                        s_Instance = assets[0] as UnityXSettings;
                    }
                    if (s_Instance == null)
                    {
                        s_Instance = CreateInstance<UnityXSettings>();
                    }
                }
                return s_Instance;
            }
        }

        public static ToolSettings toolSettings { get { return instance.m_ToolSettings; } }
        [SerializeField]
        private ToolSettings m_ToolSettings = new ToolSettings();

        public static LogSettings logSettings { get { return instance.m_LogSettings; } }
        [SerializeField]
        private LogSettings m_LogSettings = new LogSettings();

        public static PlatformSettings platformSettings { get { return instance.m_PlatformSettings; } }
        [SerializeField]
        private PlatformSettings m_PlatformSettings = new PlatformSettings();

        public static void Save()
        {
            InternalEditorUtility.SaveToSerializedFileAndForget(new Object[] { instance }, c_AssetPath, true);
        }
        public static void Update()
        {
            toolSettings.Update();
            logSettings.Update();
            platformSettings.Update();
        }
        public static void OnGUI()
        {
            EditorGUILayout.Space();
            toolSettings.OnGUI();
            EditorGUILayout.Space();
            logSettings.OnGUI();
            EditorGUILayout.Space();
            platformSettings.OnGUI();
        }

#if INIT && !UNITYX_NOT_INIT
        [UnityEditor.InitializeOnLoadMethod]
        private static void Init()
        {
            CodeFileTool.CommentsLine("UnityXSettings.cs", "#define INIT", false);
            Save();
            Update();
            UnityEditor.AssetDatabase.Refresh();
        }
#endif
    }
}
